package servlet;

import dao.impl.FinancialDaoImpl;
import entity.Financial;
import entity.User;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 财务管理(程序层)类
 */
@WebServlet("/FinancialServlet")
public class FinancialServlet extends HttpServlet {
    static FinancialDaoImpl fdi = null;

    /**
     * 处理请求并返回结果(JSON)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        if(fdi == null) fdi = new FinancialDaoImpl();

        User user = new User();
        user.setUsername("admin");

        try
        {
            String requestType = request.getParameter("type") == null ? "" : request.getParameter("type");
            System.out.println("XDLog - Request To FinancialServlet: " + requestType);
            switch (requestType)
            {
                // 获取收支详情
                case "":
                {
                    // 获取账单信息表
                    List<Financial> financials = fdi.getAllFinancialInfo(user);
                    String jsonData = JSONArray.fromObject(financials).toString();

                    jsonData = "{\"code\":0,\"msg\":\"\",\"count\":" + financials.size() + ",\"data\":"
                            + jsonData + "}";

                    response.setContentType("utf-8");
                    PrintWriter result = response.getWriter();

                    result.append(jsonData);
                } break;
                // 删除
                case "del":
                {
                    fdi.delete(request.getParameter("id"));

                    response.setContentType("utf-8");
                    PrintWriter result = response.getWriter();

                    result.append("{\"status\":true}");
                } break;
                case "ins":
                {
                    float count = Float.parseFloat(request.getParameter("lender")) - Float.parseFloat(request.getParameter("debit"));
                    Financial financial = new Financial();
                    financial.setLender_count(Float.parseFloat(request.getParameter("lender")));
                    financial.setDebit_count(Float.parseFloat(request.getParameter("debit")));
                    financial.setUsername(user.getUsername());
                    financial.setType(count>0?"income":"expend");
                    financial.setCount(count);
                    if (fdi.insert(financial)) {
                        response.setContentType("utf-8");
                        PrintWriter result = response.getWriter();
                        result.append("{\"status\":true}");
                    }
                } break;
            }


        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
